package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.JobInfoDTO;
import com.rongchuang.entity.JobInfo;
import com.rongchuang.entity.QJobInfo;
import com.rongchuang.repository.JobInfoRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class JobInfoService {

    @Autowired
    private JPAQueryFactory queryFactory;

    @Autowired
    private JobInfoRepository jobInfoRepository;

    /**
     * 分页查询
     */
    public Page<JobInfoDTO> pageSearch(int pageNum, int pageSize, String name) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QJobInfo jobInfo = QJobInfo.jobInfo;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(name)) {
            builder.and(jobInfo.jobTitle.eq(name));
        }
        List<JobInfo> jobInfos = queryFactory
                .selectFrom(jobInfo)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(jobInfo.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(jobInfo)
                .where(builder)
                .fetchCount();
        List<JobInfoDTO> jobInfoDTOS = ConverterUtil.convertToList(jobInfos, JobInfoDTO.class);
        return new PageImpl<>(jobInfoDTOS, pageable, total);
    }

    /**
     * 保存招聘信息
     */
    public void modify(JobInfoDTO dto) {
        JobInfo entity;
        if (Objects.isNull(dto.getId())) {
            entity = ConverterUtil.convertTo(dto, JobInfo.class);
            entity.setCreateTime(DateUtils.getCurrentTime());
            entity.setStatus((byte) 1);
        }else {
            entity = jobInfoRepository.getById(dto.getId());
            if (StringUtils.isNotBlank(dto.getCompanyDesc())) {
                entity.setCompanyDesc(dto.getCompanyDesc());
            }
            if (StringUtils.isNotBlank(dto.getJobDesc())) {
                entity.setJobDesc(dto.getJobDesc());
            }
            if (StringUtils.isNotBlank(dto.getJobTitle())) {
                entity.setJobTitle(dto.getJobTitle());
            }
            if (StringUtils.isNotBlank(dto.getSalaryRange())) {
                entity.setSalaryRange(dto.getSalaryRange());
            }
            if (Objects.nonNull(dto.getStatus())) {
                entity.setStatus(dto.getStatus());
            }
        }
        entity.setUpdateTime(DateUtils.getCurrentTime());
        jobInfoRepository.save(entity);
    }

    /**
     * 获取招聘信息
     * @param id 会员id
     */
    public JobInfoDTO getDetail(Long id) {
        JobInfo jobInfo = jobInfoRepository.getById(id);
        return ConverterUtil.convertTo(jobInfo, JobInfoDTO.class);
    }
}
